﻿/* -----------------------------------------------------------------------
 * <copyright file="MainPage.xaml.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang jun</author>
 * <summary>
 *            MainPage
 *          作成日：05/21/2010 
 *          新改版做成日：
 *                  05/24/2011 v2.14
 *                  
 *                  06/01/2010 ADD  控件自动显示或隐藏 Tree
 *                  07/05/2010 ADD  显示当前活动页面名称
 *                  09/17/2010 ADD  增加当前操作数据库名
 *                  09/26/2010 ADD  防止SV级用户，通过网址显示SSV级用户界面。
 *                  09/27/2010 Del  树形结构的服务及委托
 *                  04/27/2011 ADD  显示当前版本号
 * </summary>
 * ----------------------------------------------------------------------- 
 */

namespace Orid.Management.Statistics.OperatorUnitMangement
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// 控件自动显示或隐藏 Tree
    /// </summary>
    public partial class MainPage : UserControl
    {     
        /// <summary>
        /// 设置Tree区域
        /// 可见的最大值
        /// </summary>
        private GridLength gridLengthMax;
        
        /// <summary>
        /// 设置Tree区域
        /// 可见的最小值
        /// </summary>
        private GridLength gridLengthMin;

        /// <summary>
        /// 消息子窗体 errorMessbox
        /// </summary>
        private childWindow.MessageBox errorMessbox = null;

        /// <summary>
        /// 日志服务定义
        /// wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient = null;

        /// <summary>
        /// searchFlag
        /// bool
        /// </summary>
        private bool searchFlag = true;

        /// <summary>
        /// 构造函数
        /// MainPage
        /// </summary>
        public MainPage()
        {
            InitializeComponent();

            // 引用主题
            ((App)Application.Current).SetTheme((Grid)LayoutRoot);

            this.InitField();

            // 页面载入
            this.Loaded += new RoutedEventHandler(this.MainPage_Loaded);

            // 控件自动显示或隐藏 Tree
            this.btnTreeSearch.Click += new RoutedEventHandler(this.BtnTreeSearch_Click);
            this.btnTreeSearch.Content = "<";

            //点击IMGAETOOL全屏显示
            this.imageTools.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(imageTools_MouseLeftButtonDown);

            //导航 委托 09/26/2010 Add
            this.mainFrame.Navigating += new System.Windows.Navigation.NavigatingCancelEventHandler(mainFrame_Navigating);

            //this.cbSetTheme.ItemsSource = ((App)Application.Current).themeURIList;
            //this.cbSetTheme.SelectionChanged += new SelectionChangedEventHandler(cbSetTheme_SelectionChanged);

        }

        /// <summary>
        /// 导航 实现
        /// 验证请求的URI从而有选择性的进行导航
        /// （防止SV级用户，通过网址显示SSV级用户界面。）
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The  instance containing the event data.</param>
        /// <value>09/26/2010 Add</value>
        void mainFrame_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            try
            {

                if (ClientDataList.IsURIAddress.Count <= 0) return;

                //判断当前的URI是不是当前用户可用的URI
                var q = ClientDataList.IsURIAddress.Where(x => x.ToLower().Contains(e.Uri.ToString().ToLower())).ToList<string>();

                if (q.Count <= 0)
                {
                    e.Cancel = true;
                }
            }
            catch 
            {
                return;
            }
          
        }

       
        /// <summary>
        /// 变量初始化方法  InitField
        /// </summary>
        private void InitField()
        {
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfLogClient = Wcf.Service.Log4;
            this.gridLengthMax = new GridLength(160);
            this.gridLengthMin = new GridLength(20);
            this.searchFlag = true;
        }
        //void cbSetTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    ((App)Application.Current).SetSelTheme((Grid)LayoutRoot,this.cbSetTheme.SelectedItem.ToString());
        //}

        /// <summary>
        /// 点击IMGAETOOL全屏显示
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of MouseButtonEventArgs</param>
        void imageTools_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
        }

        /// <summary>
        /// GetMainFram
        /// Frame
        /// </summary>
        /// <returns>return value of Frame</returns>
        public Frame GetMainFram()
        {
            return mainFrame;
            
        }

        /// <summary>
        /// 页面载入
        /// MainPage_Loaded
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of RoutedEventArgs</param>
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //取得当前版本号  04/27/2011 -- ADD
                if (!String.IsNullOrEmpty(ClientUserInfor.UserRealName))
                {
                    this.tbActiveUserName.Text = "当前登录用户：" + ClientUserInfor.UserRealName.Trim()
                        + "  版本：" + FunctionClass.GetFormatedVersion(true)+" ";

                }
                else
                {
                    this.tbActiveUserName.Text = "当前登录用户：" + ClientUserInfor.UserName.Trim()
                        + "  版本：" + FunctionClass.GetFormatedVersion(true) + " ";
                }

                // 根据当前用户权限，来显示树形结构
                if (!String.IsNullOrEmpty(ClientUserInfor.Condition) &&
                            ClientDataList.ClientTreeInforList.Count > 0)
                {
                    // RoleID 为0，用户或为初始设置，或没有管理权
                    if (!ClientUserInfor.RoleID.Equals("0"))
                    {
                        // 形成树形结构
                       this.AddTreeNode("0", null, ClientDataList.ClientTreeInforList);
                    }
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " MainPage_Loaded " + ex.Message.ToString());
                FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "提示",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }

        }

        #region TreeView

        /// <summary>
        /// TreeView事件
        /// TreeOfList_SelectedItemChanged
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of RoutedPropertyChangedEventArgs</param>
        private void TreeOfList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = e.NewValue as TreeViewItem;
            ClientTreeInfor cti = item.DataContext as ClientTreeInfor;

            //显示当前活动页面名称  -- 07/05/2010-- add             
            if (!string.IsNullOrEmpty(cti.NodeName))
            {
                this.tbActiveUserName.Text = string.Empty;

                //只有是 权限管理页面 才显示
                if (cti.NodeParendID.EndsWith("1"))
                {
                    this.tbActiveUserName.Text += "当前数据库名：" + ClientUserInfor.DataBaseName.Trim() + "  ";
                }

                this.tbActiveUserName.Text += "当前页面：" + FunctionClass.MyIsNullOrEmpty(cti.NodeName, string.Empty)
                                            + "  " +
                                            "当前登录用户：" + ClientUserInfor.UserRealName.Trim()
                                              + "  版本：" + FunctionClass.GetFormatedVersion(true) + " ";
            }

            if (!String.IsNullOrEmpty(cti.TagUri.Trim().ToString()))
            {
                this.mainFrame.Navigate(new Uri(cti.TagUri.ToString().Trim(), UriKind.RelativeOrAbsolute));
            }
        }

        /// <summary>
        /// 形成树形结构
        /// AddTreeNode
        /// </summary>
        /// <param name="nodeParendID">前一级 结点名称 nodeParendID</param>
        /// <param name="treeViewItem">TreeViewItem treeViewItem</param>
        /// <param name="clientTreeInforList">树结构的数据源</param>
        private void AddTreeNode(string nodeParendID, TreeViewItem treeViewItem, List<ClientTreeInfor> treeInforList)
        {
            // 根据NodeParendID来查找当前一级的结点。
            List<ClientTreeInfor> result = (from treeListInfo in treeInforList
                                            where treeListInfo.NodeParendID == nodeParendID
                                            select treeListInfo).ToList<ClientTreeInfor>();

            #region 递归
            if (result.Count > 0)
            {
                foreach (ClientTreeInfor foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();
                    objTreeNode.Header = foruminfo.NodeName;
                    objTreeNode.DataContext = foruminfo;
                    objTreeNode.IsExpanded = true;

                    ////此样式将会添加的所有叶子结点上
                    ////objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    // 添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfList.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }

                    this.AddTreeNode(foruminfo.NodeID, objTreeNode, treeInforList);
                }
            }
            #endregion 
        }

        #endregion  TreeView

        #region 控件自动显示或隐藏 Tree(实现)
        /// <summary>
        /// 控件自动显示或隐藏 Tree(实现)
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of RoutedEventArgs</param>
        /// <value >06/01/2010--ADD</value>
        private void BtnTreeSearch_Click(object sender, RoutedEventArgs e)
        {
            // 隐藏
            if (this.searchFlag == true)
            {
                this.TreeColumn.Width = this.gridLengthMin;
                ((Button)sender).Content = ">";
                 
                FunctionClass.SetToolTipText((Button)sender,PlacementMode.Mouse,"显示树形菜单");
            }            
            else
            {
             // 显示
                this.TreeColumn.Width = this.gridLengthMax;
                ((Button)sender).Content = "<";

                FunctionClass.SetToolTipText((Button)sender, PlacementMode.Mouse, "隐藏树形菜单");
            }
            this.searchFlag = !this.searchFlag;
        }

        #endregion 控件自动显示或隐藏 Tree(实现)

    }
}
